<?php

namespace App\Models;

use App\Libraries\Common\CommonFun;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class RsSendingMachineMsg extends Model
{
    protected $table='rs_sending_machine_msg';


    //查询数据
    public static function lists($page = 1, $limit = 20, $params = []){
        $ary_where = array();
        $name=data_get($params,'name',false);

        if($name){
            $ary_where[] = ['name', 'like', '%'.$name.'%'];
        }
        $query = self::where($ary_where);
        $count = $query->count();
        $data = $query->offset(CommonFun::getOffset($page, $limit))->limit($limit)->orderBy('id', 'desc')->get()->map(function (RsSendingMachineMsg $rebate) use ($count) {
            $rebate = $rebate->attributesToArray();
            $rebate['status'] = $rebate['status']==0 ?'未开启' : '已开启';

            return $rebate;
        })->toArray();

        return $data ? compact('count', 'data') : false;
    }

    //对消息进行添加操作
    public static function createApi($data){

        if(! $id=data_get($data,'id',false)){
            $model=new self();
            $model->type=$data['type'];
            $model->name=$data['name'];
            $model->status=$data['status'];
            $model->webhook=$data['webhook'];
        }else{
            $model = self::where(['id'=>$id])->first();
            $model->type=$data['type'];
            $model->name=$data['name'];
            $model->status=$data['status'];
            $model->webhook=$data['webhook'];
            // $model->create_time=date('Y-m-d');
        }

        return $model->save() ?true:false;
    }


    /**
     * 格式化时间
     * */
    protected function serializeDate(\DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }


}
